<?php
/**
 * Created by PhpStorm.
 * User: Aliaksei Lazerka
 * Date: 10/7/14
 * Time: 5:17 PM
 */

namespace controller;

if (!defined('NYMPH_ROOT')) {
  define('NYMPH_ROOT', dirname("../../../" . __FILE__));
  require(NYMPH_ROOT . '/local/loader/kernel/classes/URLClassLoader.php');
}


use parser\XMLParser;

class WebParserImpl extends XMLParser
{

  /**
   * @param string $pathToXml [optional]
   */
  function __construct($pathToXml)
  {
    if (!$pathToXml) {
      $this->addFilesToParse(array("web.xml"));
    } else {
      $this->addFilesToParse(array($pathToXml));
    }
  }

  /**
   * (PHP 5)<br/>
   * Trying to parse some object
   * @param string $object
   * @return array array with parsed object
   */
  function parse($object)
  {
    $actionArray = array();

    $filterArray = array();
    $modelArray = array();
    $viewArray = array();

    $configArray = array();

    $xmlFile = simplexml_load_file($object);

    foreach ($xmlFile->filters as $filters) {
      foreach ($filters->filter as $filter) {
        $tmpFilter = null;
        if ($filter['after']) {
          $tmpFilter = new AfterFilter();
        } else {
          $tmpFilter = new Filter();
        }
        $tmpFilter->setName($filter['name']);
        $tmpFilter->setIncludes(self::getIncludes($filter->includes));
        $tmpFilter->setBodyPath($filter->body['src']);
        foreach ($filter->mappings->action as $actionM)
          $tmpFilter->addActionMapping(strval($actionM['ref']));
        if (strval($actionM['ref']) == "*") {
          foreach ($filter->mappings->action->exception as $actionException) {
            $tmpFilter->addActionMappingException(strval($actionException['ref']));
          }
        }
        $filterArray[] = $tmpFilter;
      }
    }

    foreach ($xmlFile->models as $models) {
      foreach ($models->model as $model) {
        $tmpModel = new Model();
        $tmpModel->setName($model['name']);
        $tmpModel->setIncludes(self::getIncludes($model->includes));
        $tmpModel->setBodyPath($model->body['src']);
        $name = $tmpModel->getName();
        $modelArray[strval($name)] = $tmpModel;
      }
    }


    foreach ($xmlFile->views as $views) {
      foreach ($views->view as $view) {
        $tmpView = new View();
        $tmpView->setName(strval($view['name']));
        $tmpView->setIncludes(self::getIncludes($view->includes));
        $tmpView->setBodyPath(strval($view->body['src']));
        $viewArray[strval($tmpView->getName())] = $tmpView;
      }
    }


    foreach ($xmlFile->actions as $actions) {
      foreach ($actions->action as $action) {
        $tmpAction = new Action();
        $conf = new WebConfig();
        foreach ($filterArray as $filter) {
          $actionName = strval($action['name']);
          if ($filter->checkMapping($actionName) || ($filter->checkMapping("*") && !$filter->isMappingException($actionName))) {
            $conf->addFilter($filter);
          }
        }
        $tmpAction->setName($action['name']);
        $tmpAction->setForward(strval($action->forward['ref']));
        $conf->setAction($tmpAction);
        $conf->setModel($modelArray[strval($action->model['ref'])]);
        $conf->setView($viewArray[strval($action->view['ref'])]);
        $actionArray[strval($tmpAction->getName())] = $tmpAction;
        $configArray[strval($action->mapping)] = $conf;
      }
    }

    return $configArray;
  }

  static function  getIncludes($xmlElem)
  {
    $includes = null;
    foreach ($xmlElem->include as $include) {
      $includes[] = $include;
    }
    return $includes;
  }

  function init()
  {
  }
}